﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SystemContainer.Models
{   /*
     * get some specific type of data from database
     * */
    public class DAO
    {
        private SystemEntities db = new SystemEntities();
      
        public List<AreaOfInterest> searchAOIbyCityID(int cityID) 
        {
            List<AreaOfInterest> aois =  db.Cities.Find(cityID).AreaOfInterests.ToList();
            List<AreaOfInterest> ordered = aois.OrderBy(a => a.rank).ToList();

            return ordered;
        }
        public List<AreaOfInterest> searchAOIbyBounds(int cityID, double nelat, double nelng, double swlat, double swlng)
        {
            List<AreaOfInterest> aois = db.Cities.Find(cityID).AreaOfInterests.ToList();
            List<AreaOfInterest> result = new List<AreaOfInterest>();

            foreach (AreaOfInterest a in aois)
            {
                if (a.Latitude > swlat && a.Latitude < nelat && a.Longtitude > swlng && a.Longtitude < nelng)
                    result.Add(a);
            }           
            
            List<AreaOfInterest> ordered = result.OrderBy(a => a.rank).ToList();
            return ordered;     
        
        }
        public List<City> getAllCities()
        {
            return db.Cities.ToList();
        }

        public City searchCityByCityID(int cityID)
        {
            return db.Cities.Find(cityID);
        }
        public City searchCityByCityName(String cityName)
        {
            return db.Cities.Single(c=>c.locationName==cityName);
        }

        public List<Venue> searchVenueByAOIID(int AOIID, int cityID)
        {
            return db.AreasOfInterests.Find(AOIID, cityID).Venues.ToList();
        }

        public List<ScenicLocation> searchScenicLocationByAOIID(int AOIID, int cityID)
        {
            AreaOfInterest a = db.AreasOfInterests.Find(AOIID, cityID);
            List<ScenicLocation> sls =  a.ScenicLocations.ToList();

            foreach (ScenicLocation s in sls)
            {
                s.addPicUrl();
            }
            return sls;
        }

       

        public Venue searchVenueDetail(int ID)
        {

            return db.Venues.Find(ID);
        }

        public ScenicLocation searchScenicLocationDetail(int SceID, int CityID)
        {
            ScenicLocation s = db.ScenicLocations.Find(SceID, CityID);
            s.addPicUrl();
            return s;
        }

        
        public Location searchLocationDetail(int ID, int type)
        {
        switch (type)
        {
            case (int)LocationType.Venue:
                return db.Venues.Find(ID);
            case (int)LocationType.ScenicLocation:
                return db.ScenicLocations.Find(ID);
            case (int)LocationType.City:
                return db.Cities.Find(ID);
            case (int)LocationType.AreaOfInterest:
                return db.AreasOfInterests.Find(ID);
            default:
                return null;
        }
        }

    }
}